package com.emar.spring.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Repository;

import com.emar.spring.common.util.StringUtil;
import com.emar.spring.model.UserEntity;
/**
 * 用户操作类
 * 用于对用户表的CRUD操作
 */
@Repository
public class UserDao extends BaseDAO{

	private Log log = LogFactory.getLog(UserDao.class);
	
	/**
	 * 拼接公共where查询条件
	 * @param user	UserEntity对象
	 * @return	返回一个where条件的String串
	 */
	private String getWhereSql(UserEntity user){
		StringBuffer sb = new StringBuffer();
		if(StringUtil.isNotZero( user.getId())){
			sb.append( " and u.id = '"+user.getId()+"'");
		}
		if(StringUtil.isNotEmpty(user.getAccount())){
			sb.append( " and u.account = '"+user.getAccount()+"'");
		}
		if(StringUtil.isNotEmpty(user.getRole())){
			sb.append( " and u.role = '"+user.getRole()+"'");
		}
		if(StringUtil.isNotEmpty(user.getStatus())){
			sb.append( " and u.status = '"+user.getStatus()+"'");
		}
		sb.append( " and u.status >= '0' ");
		return sb.toString().replaceFirst( "and",  "where");
	}
	
	/**
	 * 拼接查询User表集合的sql语句
	 * @param user	UserEntity对象
	 * @param pageNumber	分页的页数
	 * @param pageSize		分页数
	 * @return	
	 */
	private String getListSql(UserEntity user , int pageNumber,int pageSize){
		StringBuffer sb = new StringBuffer();
		sb.append( " SELECT ");
		sb.append( " ,u.id as id ");
		sb.append( " ,u.account as account ");
		sb.append( " ,u.password as password ");
		sb.append( " ,u.role as role ");
		sb.append( " ,u.status as status ");
		sb.append( " FROM t_user u ");
		sb.append(this.getWhereSql(user));
		return sb.toString().replaceFirst( ",",  "");
	}
	
	/**
	 * 拼接查询结果总数的sql
	 * @param user 
	 * @return
	 */
	private String getCountSql(UserEntity user){
		StringBuffer sb = new StringBuffer();
		sb.append( " SELECT ");
		sb.append( " count(*) ");
		sb.append( " FROM t_user u ");
		sb.append(this.getWhereSql(user));
		return sb.toString();
	}
	
	/**
	 * 获取查询结果数
	 * @param user
	 * @return
	 */
	public int getUserCount(UserEntity user){
		String sql = this.getCountSql(user);
		return getCount(sql);
	}
	
	/**
	 * 查询获取一个用户
	 * @param u
	 * @return
	 */
	public UserEntity getOneUser(UserEntity u) {
		String sql = this.getListSql(u, 0, 1);
		UserEntity user = getJavaBean(sql, UserEntity.class);
		return user;
	}
	
	/**
	 * 查询获取多个用户的集合，以分页方式获取
	 * @param user
	 * @param pageNumber
	 * @param pageSize
	 * @return	返回一个分页内的集合
	 */
	public List<UserEntity> getUserPageList(UserEntity user , int pageNumber,int pageSize){
		UserEntity u = new UserEntity();
		String sql = this.getListSql(user, pageNumber, pageSize);
		List<UserEntity> list = this.getPageList(sql, UserEntity.class,pageNumber,pageSize);
		return list;
	}
	
	/**
	 * 查询获取多个用户的集合
	 * @param user	
	 * @param pageNumber
	 * @param pageSize
	 * @return	返回所有查询到的结果
	 */
	public List<UserEntity> getUserList(UserEntity user , int pageNumber,int pageSize){
		StringBuffer sb = new StringBuffer();
		String sql = this.getListSql(user, pageNumber, pageSize);
		List<UserEntity> list = this.getList(sql, UserEntity.class);
		return list;
	}

	/**
	 * 添加新用户，Insert sql
	 * @param user
	 * @return
	 */
	public int insertUser(UserEntity user){
		String sql = "INSERT INTO t_user(`account`, `password`, `role`, `status`) VALUES(?,?,?,?)";
		return this.commonUpdate(sql, user.getAccount(),user.getPassword(),user.getRole(),user.getStatus());
	}
	
	/**
	 * 更新用户，update sql
	 * @param user
	 * @return
	 */
	public int updateUser(UserEntity user){
		String sql = "UPDATE t_user u SET u.`account` = ?,u.`password` = ?,u.`role` = ?,u.`status` = ?  WHERE u.`id` =?";
		return this.commonUpdate(sql, user.getAccount(),user.getPassword(),user.getRole(),user.getStatus(),user.getId());
	}
	
	/**
	 * 删除用户，delete sql ,以id删除
	 * @param user
	 * @return
	 */
	public int deleteUser(UserEntity user){
		String sql = "DELETE FROM t_user WHERE id = ?";
		return this.commonUpdate(sql, user.getId());
	}
}
